package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ProblemBackQueryContent;

/**
 * Created by lenovo on 2016/11/25.
 */
public class ProblemBackQueryFragment extends Fragment implements View.OnClickListener {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.ProblemBackQueryFragment.extra_value";
  private final int REG_DATE = 0;
  private final int IF_REPLY = 1;
  private final int DATA_STATUS = 2;

  private TextView mtvRegDate;
  private ImageView mibRegDate;
  private EditText metSubPerson;
  private TextView mtvIfReply;
  private ImageView mibIfReply;
  private EditText metBackContent;
  private ImageView mibDataStatus;
  private TextView mtvDataStatus;

  private String regDateName;
  private String regDateCode;
  private String subPerson;
  private String backContent;
  private String dataStatusName;
  private String dataStatusCode;
  private String ifReplyName;
  private String ifReplyCode;
  private ProblemBackQueryContent mqueryContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    mqueryContent = (ProblemBackQueryContent) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_confirm:
        subPerson = metSubPerson.getText().toString();
        backContent = metBackContent.getText().toString();
        //                mqueryContent = new ProblemBackQueryContent(regDateName, regDateCode, subPerson, backContent, dataStatusName, dataStatusCode, ifReplyName, ifReplyCode);
        mqueryContent.setSubPerson(subPerson);
        mqueryContent.setBackContent(backContent);
        Intent i = new Intent();
        i.putExtra(EXTRA_CONTENT, mqueryContent);
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_problem_back_query, container, false);
    findView(view);
    initData();
    return view;
  }

  private void findView(View view) {
    mtvRegDate = (TextView) view.findViewById(R.id.tv_regDate);
    mibRegDate = (ImageView) view.findViewById(R.id.ib_regDate);
    metSubPerson = (EditText) view.findViewById(R.id.et_subPerson);
    mtvIfReply = (TextView) view.findViewById(R.id.tv_ifReply);
    mibIfReply = (ImageView) view.findViewById(R.id.ib_ifReply);
    metBackContent = (EditText) view.findViewById(R.id.et_backContent);
    mibDataStatus = (ImageView) view.findViewById(R.id.ib_dataStatus);
    mtvDataStatus = (TextView) view.findViewById(R.id.tv_titleRight);
    mibIfReply.setOnClickListener(this);
    mibRegDate.setOnClickListener(this);
    mibDataStatus.setOnClickListener(this);
  }

  private void initData() {
    mtvRegDate.setText(mqueryContent.getRegDateName());
    metSubPerson.setText(mqueryContent.getSubPerson());
    metBackContent.setText(mqueryContent.getBackContent());
    mtvDataStatus.setText(mqueryContent.getDataStatusName());
    mtvIfReply.setText(mqueryContent.getIfReplyName());
  }

  @Override public void onClick(View view) {
    FragmentManager fm = getFragmentManager();
    DialogFragment dialog = null;
    int requestCode = 0;
    switch (view.getId()) {
      case R.id.ib_regDate:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.regDate);
        requestCode = REG_DATE;
        break;

      case R.id.ib_ifReply:
        //                dialog = new IsOrNotDialogFragment();
        dialog = new IfReplyDialogFragment();
        requestCode = IF_REPLY;
        break;
      case R.id.ib_dataStatus:
        dialog = new ProblemBackStatusDialogFragment();
        requestCode = DATA_STATUS;
        break;
    }
    dialog.setTargetFragment(this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REG_DATE) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      regDateName = customer.getName();
      regDateCode = customer.getId();
      mtvRegDate.setText(regDateName);
      mqueryContent.setRegDateCode(regDateCode);
      mqueryContent.setRegDateName(regDateName);
    }
    if (requestCode == IF_REPLY) {
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(IfReplyDialogFragment.EXTRA_VALUE);
      ifReplyName = content.getName();
      ifReplyCode = content.getId();
      mtvIfReply.setText(ifReplyName);
      mqueryContent.setIfReplyCode(ifReplyCode);
      mqueryContent.setIfReplyName(ifReplyName);
    }
    if (requestCode == DATA_STATUS) {
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(ProblemBackStatusDialogFragment.EXTRA_VALUE);
      dataStatusName = content.getName();
      dataStatusCode = content.getId();
      mtvDataStatus.setText(dataStatusName);
      mqueryContent.setDataStatusCode(dataStatusCode);
      mqueryContent.setDataStatusName(dataStatusName);
    }
  }

  public static ProblemBackQueryFragment newInstance(ProblemBackQueryContent content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    ProblemBackQueryFragment fragment = new ProblemBackQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
